package com.yiming.shiro.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.yiming.shiro.repository.PermissionRepository;
import com.yiming.shiro.repository.entity.Permission;
import com.yiming.shiro.service.dto.PermissionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<PermissionDTO> findPermissionByRoleId(Set<Long> roleIdSet) {
        List<Permission> permissionList = permissionRepository.findPermissionListByRoleId(roleIdSet);
        if (CollectionUtil.isEmpty(permissionList)) {
            return new ArrayList<>();
        }
        return permissionList.stream().map(permission -> {
            PermissionDTO permissionDTO = PermissionDTO.builder().build();
            BeanUtil.copyProperties(permission, permissionDTO);
            return permissionDTO;
        }).collect(Collectors.toList());
    }
}
